<template>
  <el-dialog :title=" type==='add'? '新增部门': '编辑部门'" :visible.sync="showDialog" width="500px" @close="close">
    <el-form ref="departmentRef" :model="departmentform" label-width="120px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="departmentform.name" autocomplete="off" size="small" style="width: 80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编号" prop="code">
        <el-input v-model="departmentform.code" autocomplete="off" size="small" style="width: 80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <!-- <el-input v-model="departmentform.managerId" autocomplete="off" size="small" style="width: 80%" /> -->
        <el-select v-model="departmentform.managerId" placeholder="请选择负责人" size="small" style="width: 80%">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="departmentform.introduce" autocomplete="off" size="small" type="textarea" style="width: 80%" :rows="4" placeholder="1-300个字符" /></el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="submitForm">确认提交</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </el-form-item>
    </el-form>

  </el-dialog>
</template>

<script>
import { getMannagerList, getDepartment, addDepartment, updateDepartment } from '@/api/department'
export default {
  data() {
    return {
      departmentform: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不为空', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            let result = await getDepartment()
            if (this.type === 'edit') {
              result = result.filter(item => item.name !== this.departmentform.name)
              console.log('name过滤了')
            }
            if (result.some(item => item.name === value)) {
              callback(new Error('部门名称已存在'))
            } else {
              callback()
            }
          }, trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称长度在1-50之间', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编号不为空', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            let result = await getDepartment()
            if (this.type === 'edit') {
              console.log('code过滤了')
              result = result.filter(item => item.code !== this.departmentform.code)
            }
            if (result.some(item => item.code === value)) {
              callback(new Error('部门编号已存在'))
            } else {
              callback()
            }
          }, trigger: 'blur' },
          { min: 1, max: 50, message: '部门编号长度在1-50之间', trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '部门负责人不为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不为空', trigger: 'blur' },
          { min: 1, max: 300, trigger: 'blur' }
        ]
      },
      showDialog: false,
      type: '',
      departmentRef: null,
      managerList: []
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    open(type, value) {
      console.log(type, value)
      this.type = type
      this.showDialog = true
      if (type === 'edit') {
        this.departmentform = { ...value }
      } else if (type === 'add') {
        this.departmentform.pid = value.id
      }
    },
    async submitForm() {
      if (this.type === 'add') {
        await this.$refs.departmentRef.validate()
        // console.log(this.departmentform, 'form')
        await addDepartment(this.departmentform)
        this.$message.success('添加部门成功')
      } else if (this.type === 'edit') {
        await this.$refs.departmentRef.validate()
        await updateDepartment(this.departmentform)
        this.$message.success('修改部门成功')
      }
      this.$emit('addDept')

      this.close()
    },
    close() {
      this.showDialog = false
      this.$refs.departmentRef.resetFields()
      this.departmentform = {}
    },
    async getManagerList() {
      this.managerList = await getMannagerList()
    }
  }

}
</script>

<style>

</style>
